import java.util.ArrayList;
import java.util.List;

public class work1 {
    public  static List<Integer> findAnagrams(String s, String p) {
        List<Integer>list=new ArrayList<>();
        int []arr1=new int[26];
        int []arr2=new int[26];
        for (int i = 0; i <p.length(); i++) {
            arr2[p.charAt(i)-'a']++;
        }
        int right=p.length()-1;
        if(s.length()<right+1)return list;
        for (int i = 0; i <=right ; i++) {
            arr1[s.charAt(i)-'a']++;
        }
        int count=0;
        for(int i = 0; i <p.length(); i++) {
            if(arr1[p.charAt(i)-'a']<=arr2[p.charAt(i)-'a'])count++;
        }
        int left=0;
        while (right<s.length()){
            if (count==p.length()){
                list.add(left);
            }
            if(left<=right&&arr1[s.charAt(left)-'a']<=arr2[s.charAt(left)-'a'])count--;
            arr1[s.charAt(left)-'a']--;
            left++;
            right++;
            if(right<s.length()){
                arr1[s.charAt(right)-'a']++;
                if(arr1[s.charAt(right)-'a']<=arr2[s.charAt(right)-'a'])count++;
            }
        }
        return list;
    }

    public static void main(String[] args) {
        String s="baa";
        String p="aa";
        List<Integer>list=new ArrayList<>();
        list=findAnagrams(s,p);
        System.out.println(list.toString());
    }
}
